<?php
    class Admin_NewscommentController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_news_comment;
        
         public function init(){
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_news_comment = new Admin_Model_Newscomment();
        }
        
        public function preDispatch(){
        	$auth = Zend_Auth::getInstance();
        	if(!$auth->hasIdentity()){
        		if($this->_request->getActionName() != 'login'){
        			$this->_redirect('admin/index/login');
        		}
        	}
        }
        
        public function indexAction()
        {
            $this->view->headTitle('Quản lý comment tin tức');
            $news_comment = new Admin_Model_Newscomment();
            
            if($_POST['refresh'])
            {
                $this->_redirect('admin/newscomment');
            }
            if($_POST['delete'])
            {
                $listId = $this->_arrParam['check'];
                $news_comment->deletenews_comments($listId);
                $this->_redirect($_SERVER['HTTP_REFERER']);
            }
            
            $list = $news_comment->listnews_comment_resuilt();
            $paginator = Zend_Paginator::factory($list);
            $paginator->setItemCountPerPage(40);
            $paginator->setPageRange(5);
            $currentPage = $this->_request->getParam('page', 1);
            $paginator->setCurrentPageNumber($currentPage);            
            $this->view->data = $paginator;
            $this->view->total = count($list);
        }
        
        public function deleteAction(){
            $this->_news_comment->deletenews_comment($this->_arrParam['news_comment_id']);
            $this->_redirect('admin/newscomment');
        }
        
        public function editAction(){
            $this->view->headTitle('Sửa comment tin tức');
            $news_comment_id = $this->_arrParam['news_comment_id'];
            $this->view->data = $this->_news_comment->getnews_commentById($news_comment_id);
            
            
            if($this->_request->isPost()){
                $filter = new Zend_Filter_StringTrim();
                $news_comment_content = $filter->filter($this->_arrParam['news_comment_content']);
                 $user_id = $this->_arrParam['user_id'];
                $news_id = $this->_arrParam['news_id'];
                $create_date = $this->_arrParam['create_date'];
                $status = $this->_arrParam['status'];
                $validation = new Admin_Form_ValidateNewsComment();
                $flag = 0;
                
                if($validation->validNewsCommentContent($news_comment_content)){
                    $this->view->news_comment_content = $news_comment_content;
                }
                else{
                    $this->view->error = 'Nội dung comment phải đủ 10 ký tự';
                    $flag++;
                }
                
                if($flag == 0){
                    $flex = new Zendvn_Filter_RemoveCircumflex();
                    $lower = new Zend_Filter_StringToLower();
                    $data = array(
                        'user_id' => $user_id,
                        'news_id' => $news_id,
                        'news_comment_content' => $news_comment_content,
                        'status' => $status,
                    );
                    $this->_news_comment->editnews_comment($data, $news_comment_id);
                    $data['news_comment_id'] = $news_comment_id;
                    $data['news_comment_content_filter'] = $lower->filter($flex->filter($news_comment_content));
                    $this->_news_comment->saveAddItem($data, array('task' => 'edit'));
                    $this->_redirect('admin/newscomment');
                }
            }
        }
        
       public function searchAction()
		{
			$this->view->headTitle('Tìm kiếm comment');
            $filter = new Zend_Filter_StringTrim();
            $flex   = new Zendvn_Filter_RemoveCircumflex();
            $lower  = new Zend_Filter_StringToLower();
            $flag   = 0;
            
			$keyword = $filter->filter($this->_arrParam['keyword']);
			$this->view->searchValue = $keyword;
			
            if($_POST['refresh']){
				$this->_redirect('admin/newscomment');
			}
            if($_POST['delete']){
                $listId = $this->_arrParam['check'];
				$this->_category->deletecategorys($listId);
				$this->_redirect($_SERVER['HTTP_REFERER']);
            }
			
			$validation = new Admin_Form_ValidateNewsComment();
			if($validation->validNewsCommentSearch($keyword))
			{
				$this->view->searchValue = $keyword;
			}
			else
			{
				$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
                $keyword = $lower->filter($flex->filter($keyword));
                $keyword = 'news_coment_filter:' . $keyword;
				$result = $this->_news_comment->searchcategory($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(40);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
					
				$this->view->total = count($result);
				$this->view->data = $paginator;
				$flag++;
			}
			
			if($flag == 0)
			{
                $keyword = $lower->filter($flex->filter($keyword));
                $keyword = 'news_coment_filter:' . $keyword;
				$result = $this->_news_comment->searchnews_comment($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(40);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
				
				$this->view->total = count($result);
				$this->view->data = $paginator;
			}			
		}
        
        
    }
